package com.example.zhongxinjinqiao.myapplication;

import android.annotation.SuppressLint;
import android.app.Fragment;
import android.app.FragmentTransaction;
import android.content.Context;
import android.graphics.PixelFormat;
import android.os.Bundle;
import android.support.annotation.NonNull;
import android.support.design.internal.BottomNavigationItemView;
import android.support.design.internal.BottomNavigationMenuView;
import android.support.design.widget.BottomNavigationView;
import android.support.v7.app.AppCompatActivity;
import android.util.Log;
import android.view.MenuItem;
import android.view.View;
import android.view.WindowManager;
import android.widget.TextView;

import com.example.zhongxinjinqiao.myapplication.Setting.SettingFragment;
import com.example.zhongxinjinqiao.myapplication.card.BusinessCardFragment;
import com.example.zhongxinjinqiao.myapplication.device.HomeFragment;
import com.example.zhongxinjinqiao.myapplication.find.FindFragment;

import java.lang.reflect.Field;

public class HomeActivity extends AppCompatActivity {
    private Fragment[] fragments = new Fragment[4];
    private BottomNavigationView.OnNavigationItemSelectedListener mOnNavigationItemSelectedListener
            = new BottomNavigationView.OnNavigationItemSelectedListener() {

        @Override
        public boolean onNavigationItemSelected(@NonNull MenuItem item) {
            FragmentTransaction transaction = getFragmentManager().beginTransaction();
            for (Fragment fragment : fragments) {
                if (fragment != null) {
                    transaction.hide(fragment);
                }
            }

            switch (item.getItemId()) {
                case R.id.navi_device:
                    if (fragments[0] == null) {
                        fragments[0] = new HomeFragment();
                        transaction.add(R.id.frame_content, fragments[0]);
                    }
                    else {
                        transaction.show(fragments[0]);
                    }
                    break;

                case R.id.navi_card:
                    if (fragments[1] == null) {
                        fragments[1] = new BusinessCardFragment();
                        transaction.add(R.id.frame_content, fragments[1]);
                    }
                    else {
                        transaction.show(fragments[1]);
                    }
                    break;

                case R.id.navi_non:
                   // showSendView ();
                    showPopView ();
                   return false;
                case R.id.navi_setting:
                    if (fragments[2] == null) {
                        fragments[2] = new SettingFragment();
                        transaction.add(R.id.frame_content, fragments[2]);
                    }
                    else {
                        transaction.show(fragments[2]);
                    }

                    break;
                case R.id.navi_find:
                    if (fragments[3] == null) {
                        fragments[3] = new FindFragment ();
                        transaction.add(R.id.frame_content, fragments[3]);
                    }
                    else {
                        transaction.show(fragments[3]);
                    }
            }
            transaction.commit();
            return true;
        }
    };

    private void showSendView() {
        final WindowManager windowManager= (WindowManager) getSystemService(Context.WINDOW_SERVICE);
        //获取窗口布局参数实例
        WindowManager.LayoutParams params=new WindowManager.LayoutParams();
        //设置窗口布局参数属性
        params.width= WindowManager.LayoutParams.MATCH_PARENT;
        params.height=WindowManager.LayoutParams.MATCH_PARENT;
        //设置window的显示特性
        params.flags=WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON|WindowManager.LayoutParams.FLAG_WATCH_OUTSIDE_TOUCH;
        //设置窗口半透明
        params.format= PixelFormat.TRANSLUCENT;
        //设置窗口类型
        params.type=WindowManager.LayoutParams.TYPE_APPLICATION_ATTACHED_DIALOG;
        //获取窗口布局实例
        final TextView text = new TextView ( this );
        //获取窗口布局中的按钮实例
        text.setBackgroundColor(0x99000000);
        windowManager.addView(text,params);
        text.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                windowManager.removeView(text);
            }
        });

    }

    private  void showPopView() {
        new SendCardView ( this ).show ();
    }


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_home);

        System.out.print(fragments);
        FragmentTransaction transaction = getFragmentManager().beginTransaction();

        fragments[0] = new HomeFragment();
        transaction.add(R.id.frame_content, fragments[0]);
        transaction.commit();

        BottomNavigationView navigation =  findViewById(R.id.navigation);
        this.disableShiftMode(navigation);
        navigation.setOnNavigationItemSelectedListener(mOnNavigationItemSelectedListener);
    }

    @SuppressLint("RestrictedApi")
    private  void disableShiftMode(BottomNavigationView view) {
        BottomNavigationMenuView menuView = (BottomNavigationMenuView) view.getChildAt(0);
        try {
            Field shiftingMode = menuView.getClass().getDeclaredField("mShiftingMode");
            shiftingMode.setAccessible(true);
            shiftingMode.setBoolean(menuView, false);
            shiftingMode.setAccessible(false);
            for (int i = 0; i < menuView.getChildCount(); i++) {
                BottomNavigationItemView item = (BottomNavigationItemView) menuView.getChildAt(i);
                //noinspection RestrictedApi
                item.setShiftingMode(false);
                // set once again checked value, so view will be updated
                //noinspection RestrictedApi
                item.setChecked(item.getItemData().isChecked());
            }
        } catch (NoSuchFieldException e) {
            Log.e("BNVHelper", "Unable to get shift mode field", e);
        } catch (IllegalAccessException e) {
            Log.e("BNVHelper", "Unable to change value of shift mode", e);
        }
    }

}
